
function Get-ActiveNetAdapter
{
    $adapters = Get-NetAdapter
    $activeAdapter = $adapters | Where-Object { $_.Status -eq "Up" }
    return $activeAdapter
}
$activeAdapter = Get-ActiveNetAdapter

if ($activeAdapter)
{
    Write-Output "current netcard:"
    Write-Output $activeAdapter.Name
}
else
{
    Write-Output "Can not find network,Unknown netcard!"
}


function Disable-NetAdapterByName {
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Name
    )
    $adapter = Get-NetAdapter -Name $Name
    if ($adapter) {
        Disable-NetAdapter -Name $Name -Confirm:$false
        Write-Output "netcard $Name has disabled"
    } else {
        Write-Output "disable failed ,can not find: $Name "
    }
}
function Enable-NetAdapterByName {
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $Name
    )

    $adapter = Get-NetAdapter -Name $Name

    if ($adapter) {
        Enable-NetAdapter -Name $Name -Confirm:$false
        Write-Output "netcard $Name has enabled"
    } else {
        Write-Output "enable failed, can not find: $Name "
    }
}

Disable-NetAdapterByName -Name "net1"
Enable-NetAdapterByName -Name "net2"

$currentAdapterName = "net2"
function Get-CurrentAdapterSpeed
{
    param(
        [Parameter(Mandatory=$true)]
        [string]
        $adapterName,

        [Parameter(Mandatory=$false)]
        [int]
        $interval = 4
    )

    # 初始化变量
    $startTime = Get-Date
    $endTime = $startTime + (New-TimeSpan -Seconds $interval)

    # 获取开始时网卡统计信息
    $startStats = Get-NetAdapterStatistics -Name $adapterName

    # 等待五秒钟
    Start-Sleep -Seconds $interval

    # 获取结束时网卡统计信息
    $endStats = Get-NetAdapterStatistics -Name $adapterName

    # 计算平均速率
    $totalReceivedBytes = $endStats.ReceivedBytes - $startStats.ReceivedBytes
    $totalSentBytes = $endStats.SentBytes - $startStats.SentBytes

    $averageReceivedSpeed = [Math]::Round($totalReceivedBytes / ($interval) / 1024)
    $averageSentSpeed = [Math]::Round($totalSentBytes / ($interval) / 1024)

    # 返回结果
    return @{
        ReceivedSpeed = $averageReceivedSpeed;
        SentSpeed = $averageSentSpeed;
    }
}

$speedResult = Get-CurrentAdapterSpeed -adapterName $currentAdapterName
#$receivedSpeed = $result.ReceivedSpeed
#$sentSpeed = $result.SentSpeed
Write-Output $speedResult

# init
$thresholdSpeed = 1024  # 1M/s in kb/s
$speedCheckDuration = 5
$maxLowSpeedDurationCount = 3  # 15 seconds at 5s interval
$speedBelowThresholdCount = 0
$state = "WaitingToReceiveFile"


function Switch-AdaptersIfNecessary
{
#    param(
#        [Parameter(Mandatory = $true)]
#        [string]
#        $net1Name,
#
#        [Parameter(Mandatory = $true)]
#        [string]
#        $net2Name,
#
#        [Parameter(Mandatory = $true)]
#        [int]
#        $thresholdSpeed = 1024, # 1M/s in kb/s
#
#        [Parameter(Mandatory = $true)]
#        [int]
#        $speedCheckDuration = 5,
#
#        [Parameter(Mandatory = $true)]
#        [int]
#        $maxLowSpeedDurationCount = 3  # 15 seconds at 5s interval
#    )

    $currentAdapterName = "net2"
    $state = "WaitingToReceiveFile"
    $speedBelowThresholdCount = 0

    while ($true)
    {
        $currentAdapterSpeed = Get-CurrentAdapterSpeed -adapterName $currentAdapterName
        $receivedSpeed = $result.ReceivedSpeed
        $sentSpeed = $result.SentSpeed

        Write-Host "$( Get-Date ) - Current adapter: $currentAdapterName, received speed: $receivedSpeed kb/s, sent speed: $sentSpeed kb/s"

        switch ($state)
        {
            "WaitingToReceiveFile" {
                if ($receivedSpeed -gt $thresholdSpeed)
                {
                    Write-Host "Switching to ReceivingFile state"
                    $state = "ReceivingFile"
                    $speedBelowThresholdCount = 0
                }
            }

            # 接收文件中
            "ReceivingFile" {
                if ($receivedSpeed -lt $thresholdSpeed)
                {
                    $speedBelowThresholdCount++
                    Write-Host "$( Get-Date ) - Speed below threshold. Count: $speedBelowThresholdCount"
                }
                else
                {
                    $speedBelowThresholdCount = 0
                }

                if ($speedBelowThresholdCount -ge $maxLowSpeedDurationCount)
                {
                    Write-Host "File receiving completed. Switching to FileReceived state."
                    $currentAdapterName = "net1"
                    Disable-NetAdapterByName -Name "net2"
                    Enable-NetAdapterByName -Name "net1"
                    $state = "WaitingToUploadFile"
                }
            }

            # 等待上传文件
            "WaitingToUploadFile" {
                if ($sentSpeed -gt $thresholdSpeed)
                {
                    Write-Host "Switching to UploadingFile state"
                    $state = "UploadingFile"
                    $speedBelowThresholdCount = 0
                }
            }

            # 上传文件中
            "UploadingFile" {
                if ($sentSpeed -lt $thresholdSpeed)
                {
                    $speedBelowThresholdCount++
                    Write-Host "$( Get-Date ) - Speed below threshold. Count: $speedBelowThresholdCount"
                }
                else
                {
                    $speedBelowThresholdCount = 0
                }

                if ($speedBelowThresholdCount -ge $maxLowSpeedDurationCount)
                {
                    Write-Host "File uploading completed. Switching to FileUploaded state."
                    $currentAdapterName = "net2"
                    Disable-NetAdapterByName -Name "net1"
                    Enable-NetAdapterByName -Name "net2"
                    $state = "WaitingToReceiveFile"
                }
            }
        }

        Start-Sleep -Seconds $speedCheckDuration
    }
}

Switch-AdaptersIfNecessary
